package com.desaysv.mvp

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment

/**
 * @author linzi
 * @date 2022-11-11
 */
abstract class MVPFragment<T : IView, E : IModel, K : BasePresenter<T, E>> : Fragment(),
    IView {
    /****MVP相关start****/
    abstract fun setPresenter(): K?
    private var presenter: K? = null
    fun getPresenter(): K? {
        if (presenter == null) {
            presenter = setPresenter()
        }
        return presenter
    }

    /****MVP相关end****/
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        /****MVP相关start****/
        getPresenter()
        presenter?.attach(this)
        /****MVP相关end****/
    }

    override fun onDestroyView() {
        super.onDestroyView()
        /****MVP相关start****/
        presenter?.onDestory()
        presenter = null
        /****MVP相关end****/
    }
}